/*
 * Copyright zhoujun 2021 Wicrenet, Inc. All rights reserved.
 */
package com.ruoyi.framework.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.net.UnknownHostException;

/**
 * 【】
 *
 * @author zhoujun
 * Created on 2021/11/25 17:08
 */
@Configuration
public class SubConfig {

    @Value("${netty.port}")
    private int port;

    /**
     * 接受广播消息配置 接受主题格式为：message_pub+本机ip+本程序端口
     * @param connectionFactory
     * @param msgAgreementListenerAdapter
     * @return
     * @throws UnknownHostException
     */
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter msgAgreementListenerAdapter) throws UnknownHostException {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //container.addMessageListener(msgAgreementListenerAdapter, new PatternTopic("message_pub"+ IpUtils.getHostIp()+":"+port));
        container.addMessageListener(msgAgreementListenerAdapter, new PatternTopic("message_pub"));
        return container;
    }

}
